﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using bts_webapp.Models;
using Telerik.Web.UI;


namespace bts_webapp
{
    public partial class CardManagement : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
        {
            BTSModelDataContext bts = new BTSModelDataContext();
            if (e.Item is GridEditFormItem)
            {
                UserCard uxCard = e.Item.FindControl(GridEditFormItem.EditFormUserControlID) as UserCard;
                var CardUpdate = uxCard.GetData();
                var card = this.DbContext.VIBA_CARDs.FirstOrDefault(u => u.Card_Id.ToString().ToLower() == CardUpdate.Card_Id.ToString().ToLower());
                var Thietbiten = from Vthietbi in bts.VIBA_THIET_BIs where Vthietbi.ThietBi_Id == CardUpdate.ThietBi_Id.ToString() select Vthietbi.ThietBi_Ten;
                if (card != null)
                {
                    card.Card_Id = CardUpdate.Card_Id;
                    card.Card_TenCard = CardUpdate.Card_TenCard;
                    card.Card_SoPort = CardUpdate.Card_SoPort;
                    card.Card_GhiChu = CardUpdate.Card_GhiChu;
                    card.ThietBi_Id = CardUpdate.ThietBi_Id;
                    card.Status = CardUpdate.Status;

                    this.DbContext.SubmitChanges();
                }

            }
        }

        protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
        {

        }

        protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            this.RadGrid1.DataSource = this.DbContext.VIBA_CARDs.ToList();
           
        }

        protected void RadGrid1_DeleteCommand(object sender, GridCommandEventArgs e)
        {
            if (e.Item is GridDataItem)
            {
                var CardId = (e.Item as GridDataItem)["Card_Id"].Text;
                var card = this.DbContext.VIBA_CARDs.FirstOrDefault(u => u.Card_Id.ToString().ToLower() == CardId.ToString().ToLower());
                if (card != null)
                {
                    this.DbContext.VIBA_CARDs.DeleteOnSubmit(card);
                    this.DbContext.SubmitChanges();
                }
            }
        }

        protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        {
            var card = e.Item.DataItem as VIBA_CARD;

            //insert new station
            if (e.Item is GridEditFormInsertItem)
            {

                UserCard uxCard = e.Item.FindControl(GridEditFormInsertItem.EditFormUserControlID) as UserCard;
                uxCard.EnableFieldCode(true);
                uxCard.InsertOrUpdate = true;
                uxCard.AddComboBoxItems_Thietbi();
            }
            //update user
            else if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
            {
                UserCard uxCard = e.Item.FindControl(GridEditFormItem.EditFormUserControlID) as UserCard;
                uxCard.EnableFieldCode(false);
                uxCard.AddComboBoxItems_Thietbi();
                uxCard.BindData(card);
                uxCard.InsertOrUpdate = false;
            }

            if (e.Item is GridDataItem)
            {
                var dataItem = e.Item as GridDataItem;
                dataItem["Status"].Text = GetStatus(dataItem["Status"].Text);
                dataItem["Thietbi_Ten"].Text = GetServiceName(card.ThietBi_Id.ToString());
            }
        }
        private string GetStatus(string status)
        {
            if (status == "1")
                return "<font color='blue'>Hoạt động</font>";
            return "<font color='red'>Không hoạt động</font>";
        }

        private List<VIBA_THIET_BI> _serviceList;
        public List<VIBA_THIET_BI> ServiceList
        {
            get
            {
                if (_serviceList == null)
                    _serviceList = new BTSModelDataContext().VIBA_THIET_BIs.ToList();
                return _serviceList;
            }
            set
            {
                _serviceList = value;
            }
        }

        public string GetServiceName(string serviceId)
        {
            if (serviceId == null)
                serviceId = "";
            var st = this.ServiceList.FirstOrDefault(x => x.ThietBi_Id.Trim() == serviceId.Trim());
            if (st != null)
                return st.ThietBi_Ten;
            return string.Empty;
        }

        protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
        {

        }

        protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)
        {
            if (e.Item is GridEditFormInsertItem)
            {
                UserCard uxCard = e.Item.FindControl(GridEditFormInsertItem.EditFormUserControlID) as UserCard;
                var card = uxCard.GetData();
                if (!IsExists(card.Card_Id.ToString()))
                {
                    this.DbContext.VIBA_CARDs.InsertOnSubmit(card);
                    this.DbContext.SubmitChanges();
                    uxCard.SetErrorStatus(string.Empty);
                }
                else
                {
                    uxCard.SetErrorStatus("Xin lỗi, mã card đã tồn tại.");
                    e.Canceled = true;
                }
            }
        }

        //private bool IsExists(int p)
        //{
        //    throw new NotImplementedException();
        //}

        private bool IsExists(string cardId)
        {
            using (var model = new BTSModelDataContext())
            {
                var item = model.VIBA_CARDs.FirstOrDefault(x => x.Card_Id.ToString().ToLower() == cardId.ToLower());
                return (item != null);
            }
        }
    }
}